iT邦幫忙

2023 iThome 鐵人賽

DAY 15
0
Mobile Development

App從開發到上架系列 第 15

Day16: iOS 開發:畫面功能(最新消息) - Notification

  • 分享至 

  • xImage
  •  

AppDelegate

import UIKit
import Firebase
import FirebaseAuth
import GoogleSignIn
import UserNotifications
@main
class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate {


    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.badge, .banner, .list, .sound]) //app內推播想要看到的資訊
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UserNotifications.UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.carPlay,.sound]) { (granted, error) in
            if granted {
                print("允許開啟")
                UNUserNotificationCenter.current().delegate = self
            }else{
                print("拒絕接受開啟")
            }
        }
        FirebaseApp.configure() // 加入這一行
        return true
    }
    
    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any]) -> Bool {
        return GIDSignIn.sharedInstance.handle(url)
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

vc加入這段:

    func createNotification () {
        let content = UNMutableNotificationContent()
        content.title = "testTitle"
        content.subtitle = "testSubTitle"
        content.body = "testBody"
        content.badge = 1
        content.sound = UNNotificationSound.defaultCritical
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
        let uuidString = UUID().uuidString
        let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }

上一篇
Day15: iOS 開發:畫面建構(最新消息) - NewsViewController
下一篇
Day18: iOS 開發:畫面功能(電子錢包) - 金流的SandBox
系列文
App從開發到上架30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言